<?php ob_start();?>
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include($root.'/standart/head.php');
include($root.'/standart/dateHeader.php');
include($root.'/standart/checkUser.php');
$db = connect();
$bgn_date = "";
$end_date = "";
$tmpDate = $_GET["begin_date"];
if($tmpDate <> ""){
	list($gun,$ay,$yil) = explode('/',$tmpDate);
	$bgn_date = $yil.'-'.$ay.'-'.$gun;
}
$tmpDate = $_GET["end_date"];
if($tmpDate <> ""){
	list($gun,$ay,$yil) = explode('/',$tmpDate);
	$end_date = $yil.'-'.$ay.'-'.$gun;
}
$invoiceNo = $_GET["invoice_no"];
$inventory = $_GET["inventory"];
$stmt ="SELECT INVENTORY_NAME, SUM(TOTAL_AMOUNT) AS AMOUNT
		FROM MATERIALS, INVENTORY
		WHERE MATERIALS.INVENTORY_ID = INVENTORY.ID";
		if($bgn_date <> ""){
			$stmt = $stmt." AND INVOICE_DATE > '$bgn_date'";
		}
		if($end_date <> ""){
			$stmt = $stmt." AND INVOICE_DATE < '$end_date'";
		}
		if($invoiceNo <> ""){
			$stmt = $stmt." AND INVOICE_NO = '$invoiceNo'";
		}
		if($inventory <> ""){
			$stmt = $stmt." AND INVENTORY_ID = '$inventory'";
		}
		$stmt = $stmt." GROUP BY INVENTORY_NAME";
		$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
	?>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../font-awesome/css/font-awesome.css" rel="stylesheet">
    <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/tinyBox.css" rel="stylesheet" type="text/css" media="screen"/>
    <link href="../css/jquery-ui-1.10.4.css" rel="stylesheet" type="text/css" media="screen"/>
	<link href="../css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
    <script type="text/javascript">
    	function loadPage() {
			Morris.Donut({
		        element: 'morris-donut-chart',
		        data: [
				<?php 
				$loopFirst = true;
				while($row = $res->fetch_assoc()){
					if($loopFirst){
						echo '{';
						$loopFirst = false;
					}else{
						echo ',{';
					}				
					echo  'label: "'.$row['INVENTORY_NAME'].' Alışı",
		            		value: '.$row['AMOUNT'];
		        	echo '}';
		        }?> 
		        ],
		        resize: true
		    });
		}

    </script>
</head>

<body onload="loadPage()">
            <!-- /.row -->
    <div id="wrapper">
			<div class="panel panel-default">
				<div class="panel-heading">
					<i class="fa fa-bar-chart-o fa-fw"></i> Toplam Alış Raporu
				</div>
				<div class="panel-body">
					<div id="morris-donut-chart"></div>
				</div>
				<!-- /.panel-body -->
			</div>
	</div>
	<!-- /.row -->
		
</body>
    <script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="../js/jquery-ui-1.10.4.js"></script>
    <script type="text/javascript" src="../js/plugins/morris/morris.js"></script>
    <script type="text/javascript" src="../js/plugins/morris/raphael-2.1.0.min.js"></script>
</html>
<?php 
$res->close();
disconnect($db);
?>
<?php ob_end_flush(); ?>